
//
//  ST_PushOptionViewController.swift
//  btvideo
//
//  Created by mac on 2020/7/22.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes

class ST_PushOptionViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(16)
            make.height.equalTo(354+BTSafeAreaBottom+16)
        }
        view.addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(root_view.snp.top).offset(-8)
        }
        
        root_view.addSubview(cancel_btn)
        cancel_btn.snp.makeConstraints { (make) in
            make.width.equalTo(80/375.0*ScreenWidth)
            make.centerX.equalToSuperview()
            make.height.equalTo(44)
            make.bottom.equalToSuperview().offset(-16-20-BTSafeAreaBottom)
        }
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(22)
            make.top.equalToSuperview().offset(20)
        }
        
//        let offset:CGFloat = 105/2.0/375.0*ScreenWidth
        
        root_view.addSubview(push_peculiarity_btn)
        push_peculiarity_btn.snp.makeConstraints { (make) in
            make.height.equalTo(80)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.top.equalToSuperview().offset(62)
        }
        
        root_view.addSubview(push_daily_btn)
        push_daily_btn.snp.makeConstraints { (make) in
            make.left.right.height.equalTo(push_peculiarity_btn)
            make.top.equalToSuperview().offset(157)
        }
        
        root_view.addSubview(face_status_label)
        face_status_label.snp.makeConstraints { (make) in
            make.left.equalTo(push_peculiarity_btn)
            make.top.equalTo(push_daily_btn.snp.bottom).offset(15)
            make.height.equalTo(18)
        }
        
        if FACE_AVATAE_STATE != 3{
            
            root_view.addSubview(detect_face_btn)
            detect_face_btn.snp.makeConstraints { (make) in
                make.left.equalTo(face_status_label).offset(40)
                make.height.equalTo(40)
                make.width.equalTo(80)
                make.centerY.equalTo(face_status_label)
            }
        }
        
    }
    
    
    
    
//    MARK: - Properties
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = .white
        control.cornerRadius = 16
        return control
    }()
    
    fileprivate lazy var cancel_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
       control.setTitleForAllStates("取消")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.backgroundColor = UIColor.init(hexString: "#DFE3E9")
        control.setTitleColorForAllStates(UIColor.white)
        control.cornerRadius = 22
        control.add_touch_up(insideTarget: self, action: #selector(cancel_handle))
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "发布")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        return label
    }()
    
    fileprivate lazy var push_peculiarity_btn:ST_PushSheetView = {
            
        let control = ST_PushSheetView()
        
        control.model = ST_PushSheetViewModel.createPec()
        
        control.addTarget(self, action: #selector(push_peculiarity_handle), for: .touchUpInside)
        return control
    }()
    
    fileprivate lazy var push_daily_btn:ST_PushSheetView = {
            
        let control = ST_PushSheetView()
        
        control.model = ST_PushSheetViewModel.createDaily()
        
        control.addTarget(self, action: #selector(push_daily_handle), for: .touchUpInside)
        return control
    }()
    
    fileprivate lazy var close_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.addTarget(self, action: #selector(cancel_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var face_status_label:UILabel = {
        
        let label = UILabel()
        
        if FACE_AVATAE_STATE == 3{
            
            label.attributedText =   "已通过真人认证，你的特质和日常会获得更多曝光与推荐！".withAttributes([Attribute.textColor(Color.init(hex: 0x8A909F)!),Attribute.font(Font.systemFont(ofSize: 13, weight: UIFont.Weight.regular))])
        }else{
            
            label.attributedText =    "去完成".withAttributes([Attribute.textColor(Color.init(hex: 0x8A909F)!),Attribute.font(Font.systemFont(ofSize: 13, weight: UIFont.Weight.regular))]) + "真人认证".withAttributes([Attribute.textColor(Color.init(hex: 0xFEC207)!),Attribute.font(Font.systemFont(ofSize: 13, weight: UIFont.Weight.regular))]) + "，获得更多曝光与推荐，让你更受欢迎！".withAttributes([Attribute.textColor(Color.init(hex: 0x8A909F)!),Attribute.font(Font.systemFont(ofSize: 13, weight: UIFont.Weight.regular))])
        }
        return label
    }()
    
    
    fileprivate lazy var detect_face_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
//        control.backgroundColor = UIColor.init(white: 0.5, alpha: 0.2)
        control.addTarget(self, action: #selector(action_handle0), for: UIControl.Event.touchUpInside)
        return control
    }()
    
}

extension ST_PushOptionViewController{
    
    
    @objc fileprivate func push_daily_handle(){
        
        goback_previous_controller(animated: false) {
            
            let vc = ST_SelectDailyMomentController()
            
            vc.title = "选择日常类型"
            
            ST_BTLaunchController.shared.navigationController?.pushViewController(vc)
        }
    }
    
    @objc fileprivate func push_peculiarity_handle(){
        
        
        
        goback_previous_controller(animated: false) {
            
            push_to_push_peculiarity_controller(navigationController: ST_BTLaunchController.shared.navigationController)
        }
    }
    
    @objc fileprivate func action_handle0(){
        
        goback_previous_controller(animated: false) {
            
            if FACE_AVATAE_STATE == 2{
              
                let failure = ST_MatchFaceFailureController()
                
                failure.overCurrentContext = true
                
                GlobalPresent(toController: failure)
                
                return
                
            }
            
            if FACE_AVATAE_STATE == 1{
                DetectFaceHandle()
            }
            
        }
        
    }
    
    @objc fileprivate func cancel_handle(){
        
        goback_previous_controller()
        
    }
}



//MARK: - Item
class ST_PushSheetView:UIControl{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(22)
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(17)
        }
        
        self.addSubview(btn)
        btn.snp.makeConstraints { (make) in
            make.height.equalTo(32)
            make.width.equalTo(86)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15)
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.height.equalTo(17)
            make.top.equalToSuperview().offset(46)
            make.left.equalToSuperview().offset(15)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var imageView:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          return control
      }()
    
    fileprivate lazy var btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.isUserInteractionEnabled = false
        
        control.cornerRadius = 16
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        
        control.backgroundColor = .white
        
        return control
    }()
    
    
    var model:ST_PushSheetViewModel?{
        didSet{
            
            guard let m = model else{
                return
            }
            
            imageView.image = m.backgroundImage
            titleLabel.text = m.title
            content_label.text = m.content
            btn.setTitleForAllStates(m.buttonStr)
            btn.setTitleColorForAllStates(m.tintColor)
            
        }
    }
}



struct ST_PushSheetViewModel{
    
    var title:String
    
    var content:String
    
    var buttonStr:String
    
    var tintColor:UIColor
    
    var backgroundImage:UIImage
    
    static func createPec()->ST_PushSheetViewModel{
        
        
        var str = "添加3个及以上【我的特质】"
        
        var content = "收获喜欢和聊天的几率会至少增加2倍哦！"
        
        if ST_BTUserInfoManager.shared.additional_attributes?.countOfMyPeculiarityPassed ?? 0 >= 3{
            
            str = "添加更多【我的特质】"
            
            content = "更多的特质能够更好的全方位展现自己！"
        }
        
        return ST_PushSheetViewModel.init(title: str, content: content, buttonStr: "立即添加", tintColor: UIColor.init(hex: 0xFEC207)!,backgroundImage:ST_ImageUtils.imageName("push_options_0")!)
    }
    
    static func createDaily()->ST_PushSheetViewModel{
        
        return ST_PushSheetViewModel.init(title: "每日发布【我的日常】", content: "记录生活中的美好，让对方更了解你！", buttonStr: "立即发布", tintColor: UIColor.init(hex: 0x42CEDA)!,backgroundImage:ST_ImageUtils.imageName("push_options_1")!)
    }
}
